package com.cloud.admin.controller.tool;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import com.cloud.admin.config.AliOssConfig;
import com.cloud.admin.controller.BaseController;
import com.cloud.common.oss.AliOssRequest;
import com.cloud.common.oss.AliOssResponse;
import com.cloud.common.oss.AliUploadHelper;
import com.cloud.common.util.R;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @Name UploadDemoController
 * @Description TODO
 * @Author douhaichao
 * @Date 2019/3/28 17:07
 * @Version 1.0
 **/
@AllArgsConstructor
@RestController
@RequestMapping("/tool/upload")
public class UploadController extends BaseController {
    private final AliOssConfig aliOssConfig;

    @PostMapping("uploadimg")
    public R uploadimg(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request) throws IOException {
        String filedir = "" + request.getParameter("filedir") + "/" + DateUtil.today().replace("-", "/") + "/" + UUID.randomUUID().toString() + ".jpg";
        FileInputStream inputStream = (FileInputStream) multipartFile.getInputStream();
//        String path = uploadQNImg(inputStream, filedir);
//        FileInputStream inputStream = (FileInputStream) multipartFile.getInputStream();
        AliOssRequest aliOssRequest = new AliOssRequest();
        BeanUtil.copyProperties(aliOssConfig, aliOssRequest);
        AliUploadHelper aliUploadHelper = new AliUploadHelper(aliOssRequest);
        AliOssResponse aliOssResponse =
                aliUploadHelper.uploadSimple(inputStream, filedir);
        return new R<>(aliOssResponse.getAbsolutePath());
    }
}
